/**
 * ProductOverEffect
 * @Chunning.Xu
 * 2011 Nov 23, 2011 11:37:02 AM
 **/
package com.lux.product.views
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	
	import org.hamcrest.Matcher;
	
	public class ProductOverEffect extends MovieClip
	{
		//START.
		
		private var rect:Rectangle;
		private var effetList:Vector.<Sprite>;
		
		private const ITEM_CLASSES:Array = [ProductEffect_1,ProductEffect_2,ProductEffect_3];
		private const MAX_EFFECT_ITEM_COUNT:int = 50;
		
		public function ProductOverEffect(rect:Rectangle)
		{
			this.rect = rect;
			
			this.mouseEnabled = false;
			this.mouseChildren = false;
			
			effetList = new Vector.<Sprite>;
		}
		
		private function enterFrameHandler(evt:Event):void
		{
			if(effetList.length < MAX_EFFECT_ITEM_COUNT)
			{
				var item:MovieClip =  new ITEM_CLASSES[Math.floor(ITEM_CLASSES.length * Math.random())];
				item.scaleX = Math.random();
				item.scaleY = item.scaleX;
				item.alpha = Math.random();
				item.y = rect.bottom;
				item.x = rect.x + rect.width * Math.random();
				item["speed"] = Math.random() * 2 + 0.5;
				item["w"] = Math.PI / rect.height * (Math.random() * 2);
				item["a"] = rect.width / 2 * Math.random();
				item["b"] = item.x;
				
				effetList.push(item);
				addChild(item);
			}
			
			for each(var effectItem:Sprite in effetList)
			{
				effectItem.y -= effectItem["speed"];
				effectItem.x = Math.sin(effectItem.y * effectItem["w"]) * effectItem["a"] + effectItem["b"];

				effectItem.alpha *= 0.99;
				
				if(effectItem.alpha < 0.1)
				{
					removeChild(effectItem);
					effetList.splice(effetList.indexOf(effectItem),1);
				}
			}
		}
		
		public override function play():void
		{
			addEventListener(Event.ENTER_FRAME,enterFrameHandler);	
		}
		
		public override function stop():void
		{
			removeEventListener(Event.ENTER_FRAME,enterFrameHandler);	
			
		 	while(numChildren > 0)
				removeChildAt(0);
			effetList = new Vector.<Sprite>;
		}
		
		//END
	}
}